package com.miniso.robot.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.miniso.robot.bean.FileMessage;
import com.miniso.robot.bean.ImageMessage;
import com.miniso.robot.bean.NewsCardMessage;
import com.miniso.robot.feign.RobotFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author guojunwang
 * @date 2021-10-18 16:47
 */
@RequestMapping("/youdata")
@RestController
public class YouDataController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RobotFeign robotFeign;



    @PostMapping("/{robotKey}")
    public String send(@PathVariable("robotKey") String robotKey ,@RequestBody  String text ) throws IOException {

        JSONObject jsonObject = JSONUtil.parseObj(text);

        JSONObject data = jsonObject.getJSONObject("data");

        ImageMessage message = new ImageMessage();
        message.setImageInfo(data.get("pictureBase64",String.class),data.get("pictureMd5",String.class));

        String result = robotFeign.sendMessage(robotKey,message);

        JSONObject resultObj = JSONUtil.parseObj(result);

        if(StrUtil.equals(resultObj.get("errcode",String.class),"0")){
            return "{\"code\":0,\"msg\":\"ok\"}";
        }else{
            return result;
        }
    }

}
